extends Node

const DURACTION_GAME_SEC := 2 * 60
const DURACTION_IMPACT_PAUSE := 100

enum State {
	IN_PLAY,
	SCORED,
	RESET,
	KICKOFF,
	OVERTIME,
	GAMEOVER
}

var current_match : Match = null
var current_state : GameState = null

#var countries : Array[String] = ["FRANCE","USA"]
#var score : Array[int]= [ 0 , 0 ]
var player_setup : Array[String] = ["FRANCE",'USA']
var state_factory := GameStateFactory.new()
var time_left : float
var time_since_pause:= Time.get_ticks_msec()


func _init() -> void:
	process_mode = ProcessMode.PROCESS_MODE_ALWAYS

func _ready() -> void:
	GameEvents.impact_received.connect(on_impact_received.bind())
	

func _process(delta: float) -> void:
	if get_tree().paused and Time.get_ticks_msec() - time_since_pause > DURACTION_IMPACT_PAUSE:
		get_tree().paused = false

func start_game() -> void:
	time_left = DURACTION_GAME_SEC 
	switch_state(State.RESET)

func switch_state(state : State,data: GameStateData = GameStateData.new()) ->void:
	if(current_state != null):
		current_state.queue_free()
	current_state = state_factory.get_fresh_state(state)
	current_state.setup(self, data)
	current_state.state_transaction_requested.connect(switch_state.bind())
	current_state.name = "GameStateMachine:" + State.keys()[state]
	
	call_deferred("add_child",current_state)

func is_coop() -> bool:
	return player_setup[0] == player_setup[1]
	
func is_single_player() -> bool:
	return player_setup[1].is_empty()

func is_time_up() -> bool:
	return time_left <= 0;

func get_winner_country() -> String:
	assert(not current_match.is_tied())
	return current_match.winner

func increase_score(country_scored_on : String) -> void:
	current_match.increase_score(country_scored_on)
	GameEvents.score_changed.emit()


func on_impact_received(_impact_position : Vector2, is_hight_impact:bool) -> void:
	if is_hight_impact:
		time_since_pause = Time.get_ticks_msec()
		get_tree().paused = true
